package com.youyi.yyviewsdklibrary.Dialog.widget;

import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import com.youyi.yyviewsdklibrary.Dialog.enums.LayoutStatus;
import com.youyi.yyviewsdklibrary.Dialog.enums.PopupPosition;
import com.youyi.yyviewsdklibrary.Dialog.util.XPopupUtils;

public class PopupDrawerLayout extends FrameLayout {
    ViewDragHelper.Callback callback;
    boolean canChildScrollLeft;
    float downX;
    float downY;
    ViewDragHelper dragHelper;
    public boolean enableDrag;
    float fraction;
    boolean hasLayout;
    public boolean isDismissOnTouchOutside;
    boolean isIntercept;
    boolean isToLeft;
    private OnCloseListener listener;
    View mChild;
    View placeHolder;
    public PopupPosition position;
    LayoutStatus status;
    float ty;
    float x;
    float y;

    public interface OnCloseListener {
        void onClose();

        void onDrag(int i, float f, boolean z);

        void onOpen();
    }

    public PopupDrawerLayout(Context context) {
        this(context, null);
    }

    public PopupDrawerLayout(Context context, AttributeSet attributeSet) {
        this(context, attributeSet, 0);
    }

    public PopupDrawerLayout(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.status = null;
        this.position = PopupPosition.Left;
        this.fraction = 0.0f;
        this.enableDrag = true;
        this.hasLayout = false;
        this.isIntercept = false;
        ViewDragHelper.Callback r2 = new ViewDragHelper.Callback() {
            /* class com.youyi.yyviewsdklibrary.Dialog.widget.PopupDrawerLayout.AnonymousClass1 */

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public int getViewHorizontalDragRange(View view) {
                return 1;
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public boolean tryCaptureView(View view, int i) {
                return !PopupDrawerLayout.this.dragHelper.continueSettling(true) && PopupDrawerLayout.this.status != LayoutStatus.Close;
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public int clampViewPositionHorizontal(View view, int i, int i2) {
                if (view == PopupDrawerLayout.this.placeHolder) {
                    return i;
                }
                return PopupDrawerLayout.this.fixLeft(i);
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public void onViewPositionChanged(View view, int i, int i2, int i3, int i4) {
                super.onViewPositionChanged(view, i, i2, i3, i4);
                if (view == PopupDrawerLayout.this.placeHolder) {
                    PopupDrawerLayout.this.placeHolder.layout(0, 0, PopupDrawerLayout.this.placeHolder.getMeasuredWidth(), PopupDrawerLayout.this.placeHolder.getMeasuredHeight());
                    PopupDrawerLayout popupDrawerLayout = PopupDrawerLayout.this;
                    int fixLeft = popupDrawerLayout.fixLeft(popupDrawerLayout.mChild.getLeft() + i3);
                    PopupDrawerLayout.this.mChild.layout(fixLeft, PopupDrawerLayout.this.mChild.getTop(), PopupDrawerLayout.this.mChild.getMeasuredWidth() + fixLeft, PopupDrawerLayout.this.mChild.getBottom());
                    calcFraction(fixLeft, i3);
                    return;
                }
                calcFraction(i, i3);
            }

            private void calcFraction(int i, int i2) {
                if (PopupDrawerLayout.this.position == PopupPosition.Left) {
                    PopupDrawerLayout popupDrawerLayout = PopupDrawerLayout.this;
                    popupDrawerLayout.fraction = (((float) (popupDrawerLayout.mChild.getMeasuredWidth() + i)) * 1.0f) / ((float) PopupDrawerLayout.this.mChild.getMeasuredWidth());
                    if (!(i != (-PopupDrawerLayout.this.mChild.getMeasuredWidth()) || PopupDrawerLayout.this.listener == null || PopupDrawerLayout.this.status == LayoutStatus.Close)) {
                        PopupDrawerLayout.this.status = LayoutStatus.Close;
                        PopupDrawerLayout.this.listener.onClose();
                    }
                } else if (PopupDrawerLayout.this.position == PopupPosition.Right) {
                    PopupDrawerLayout popupDrawerLayout2 = PopupDrawerLayout.this;
                    popupDrawerLayout2.fraction = (((float) (popupDrawerLayout2.getMeasuredWidth() - i)) * 1.0f) / ((float) PopupDrawerLayout.this.mChild.getMeasuredWidth());
                    if (!(i != PopupDrawerLayout.this.getMeasuredWidth() || PopupDrawerLayout.this.listener == null || PopupDrawerLayout.this.status == LayoutStatus.Close)) {
                        PopupDrawerLayout.this.status = LayoutStatus.Close;
                        PopupDrawerLayout.this.listener.onClose();
                    }
                }
                if (PopupDrawerLayout.this.listener != null) {
                    PopupDrawerLayout.this.listener.onDrag(i, PopupDrawerLayout.this.fraction, i2 < 0);
                    if (PopupDrawerLayout.this.fraction == 1.0f && PopupDrawerLayout.this.status != LayoutStatus.Open) {
                        PopupDrawerLayout.this.status = LayoutStatus.Open;
                        PopupDrawerLayout.this.listener.onOpen();
                    }
                }
            }

            @Override // android.support.v4.widget.ViewDragHelper.Callback
            public void onViewReleased(View view, float f, float f2) {
                int i = 0;
                int measuredWidth = 0;
                super.onViewReleased(view, f, f2);
                if (view == PopupDrawerLayout.this.placeHolder && f == 0.0f) {
                    if (PopupDrawerLayout.this.isDismissOnTouchOutside) {
                        PopupDrawerLayout.this.close();
                    }
                } else if (view != PopupDrawerLayout.this.mChild || !PopupDrawerLayout.this.isToLeft || PopupDrawerLayout.this.canChildScrollLeft || f >= -500.0f) {
                    if (PopupDrawerLayout.this.position == PopupPosition.Left) {
                        if (f < -1000.0f) {
                            measuredWidth = PopupDrawerLayout.this.mChild.getMeasuredWidth();
                        } else {
                            if (PopupDrawerLayout.this.mChild.getLeft() < (-PopupDrawerLayout.this.mChild.getMeasuredWidth()) / 2) {
                                measuredWidth = PopupDrawerLayout.this.mChild.getMeasuredWidth();
                            } else {
                                i = 0;
                            }
                        }
                        i = -measuredWidth;
                    } else if (f > 1000.0f) {
                        i = PopupDrawerLayout.this.getMeasuredWidth();
                    } else {
                        i = view.getLeft() < PopupDrawerLayout.this.getMeasuredWidth() - (PopupDrawerLayout.this.mChild.getMeasuredWidth() / 2) ? PopupDrawerLayout.this.getMeasuredWidth() - PopupDrawerLayout.this.mChild.getMeasuredWidth() : PopupDrawerLayout.this.getMeasuredWidth();
                    }
                    PopupDrawerLayout.this.dragHelper.smoothSlideViewTo(PopupDrawerLayout.this.mChild, i, view.getTop());
                    ViewCompat.postInvalidateOnAnimation(PopupDrawerLayout.this);
                } else {
                    PopupDrawerLayout.this.close();
                }
            }
        };
        this.callback = r2;
        this.isDismissOnTouchOutside = true;
        this.dragHelper = ViewDragHelper.create(this, r2);
    }

    public void setDrawerPosition(PopupPosition popupPosition) {
        this.position = popupPosition;
    }

    /* access modifiers changed from: protected */
    public void onFinishInflate() {
        super.onFinishInflate();
        this.placeHolder = getChildAt(0);
        this.mChild = getChildAt(1);
    }

    /* access modifiers changed from: protected */
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        this.ty = getTranslationY();
    }

    /* access modifiers changed from: protected */
    public void onLayout(boolean z, int i, int i2, int i3, int i4) {
        this.placeHolder.layout(0, 0, getMeasuredWidth(), getMeasuredHeight());
        if (!this.hasLayout) {
            if (this.position == PopupPosition.Left) {
                View view = this.mChild;
                view.layout(-view.getMeasuredWidth(), 0, 0, getMeasuredHeight());
            } else {
                this.mChild.layout(getMeasuredWidth(), 0, getMeasuredWidth() + this.mChild.getMeasuredWidth(), getMeasuredHeight());
            }
            this.hasLayout = true;
            return;
        }
        View view2 = this.mChild;
        view2.layout(view2.getLeft(), this.mChild.getTop(), this.mChild.getRight(), this.mChild.getMeasuredHeight());
    }

    /* JADX WARNING: Code restructure failed: missing block: B:18:0x0042, code lost:
        if (r0 != 3) goto L_0x006e;
     */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public boolean onInterceptTouchEvent(MotionEvent r6) {
        /*
        // Method dump skipped, instructions count: 164
        */
        throw new UnsupportedOperationException("Method not decompiled: com.youyi.yyviewsdklibrary.Dialog.widget.PopupDrawerLayout.onInterceptTouchEvent(android.view.MotionEvent):boolean");
    }

    private boolean canScroll(ViewGroup viewGroup, float f, float f2, int i) {
        for (int i2 = 0; i2 < viewGroup.getChildCount(); i2++) {
            View childAt = viewGroup.getChildAt(i2);
            int[] iArr = new int[2];
            childAt.getLocationInWindow(iArr);
            if (XPopupUtils.isInRect(f, f2, new Rect(iArr[0], iArr[1], iArr[0] + childAt.getWidth(), iArr[1] + childAt.getHeight())) && (childAt instanceof ViewGroup)) {
                if (childAt instanceof ViewPager) {
                    ViewPager viewPager = (ViewPager) childAt;
                    if (i != 0) {
                        return viewPager.canScrollHorizontally(i);
                    }
                    if (viewPager.canScrollHorizontally(-1) || viewPager.canScrollHorizontally(1)) {
                        return true;
                    }
                    return false;
                } else if (!(childAt instanceof HorizontalScrollView)) {
                    return canScroll((ViewGroup) childAt, f, f2, i);
                } else {
                    HorizontalScrollView horizontalScrollView = (HorizontalScrollView) childAt;
                    if (i != 0) {
                        return horizontalScrollView.canScrollHorizontally(i);
                    }
                    if (horizontalScrollView.canScrollHorizontally(-1) || horizontalScrollView.canScrollHorizontally(1)) {
                        return true;
                    }
                    return false;
                }
            }
        }
        return false;
    }

    private boolean canScroll(ViewGroup viewGroup, float f, float f2) {
        return canScroll(viewGroup, f, f2, 0);
    }

    public boolean onTouchEvent(MotionEvent motionEvent) {
        if (!this.enableDrag) {
            return super.onTouchEvent(motionEvent);
        }
        if (this.dragHelper.continueSettling(true)) {
            return true;
        }
        this.dragHelper.processTouchEvent(motionEvent);
        return true;
    }

    /* access modifiers changed from: private */
    /* access modifiers changed from: public */
    private int fixLeft(int i) {
        if (this.position == PopupPosition.Left) {
            if (i < (-this.mChild.getMeasuredWidth())) {
                i = -this.mChild.getMeasuredWidth();
            }
            if (i > 0) {
                return 0;
            }
            return i;
        } else if (this.position != PopupPosition.Right) {
            return i;
        } else {
            if (i < getMeasuredWidth() - this.mChild.getMeasuredWidth()) {
                i = getMeasuredWidth() - this.mChild.getMeasuredWidth();
            }
            return i > getMeasuredWidth() ? getMeasuredWidth() : i;
        }
    }

    public void computeScroll() {
        super.computeScroll();
        if (this.dragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    /* access modifiers changed from: protected */
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void open() {
        post(new Runnable() {
            /* class com.youyi.yyviewsdklibrary.Dialog.widget.PopupDrawerLayout.AnonymousClass2 */

            public void run() {
                PopupDrawerLayout.this.dragHelper.smoothSlideViewTo(PopupDrawerLayout.this.mChild, PopupDrawerLayout.this.position == PopupPosition.Left ? 0 : PopupDrawerLayout.this.mChild.getLeft() - PopupDrawerLayout.this.mChild.getMeasuredWidth(), 0);
                ViewCompat.postInvalidateOnAnimation(PopupDrawerLayout.this);
            }
        });
    }

    public void close() {
        post(new Runnable() {
            /* class com.youyi.yyviewsdklibrary.Dialog.widget.PopupDrawerLayout.AnonymousClass3 */

            public void run() {
                PopupDrawerLayout.this.dragHelper.abort();
                PopupDrawerLayout.this.dragHelper.smoothSlideViewTo(PopupDrawerLayout.this.mChild, PopupDrawerLayout.this.position == PopupPosition.Left ? -PopupDrawerLayout.this.mChild.getMeasuredWidth() : PopupDrawerLayout.this.getMeasuredWidth(), 0);
                ViewCompat.postInvalidateOnAnimation(PopupDrawerLayout.this);
            }
        });
    }

    public void setOnCloseListener(OnCloseListener onCloseListener) {
        this.listener = onCloseListener;
    }
}
